package com.elite.groovy.callback


import com.elite.groovy.kh.KHUtil
import com.elite.groovy.util.EliteBean
import com.elite.groovy.util.Util
import net.sf.json.JSONObject
import org.apache.commons.logging.Log
import org.apache.commons.logging.LogFactory
import org.springframework.jdbc.core.JdbcTemplate

import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse

/**
 *
 * AICC呼叫中心事件推送
 * 目前支持的事件有：来电事件、外呼事件、座席状态变动事件、挂机事件等；推送的内容包括来电信息、座席状态、话单信息、满意度信息等
 *
 */

public class apiClinkCallback implements ServiceBackInvoke {
    private static final Log log = LogFactory.getLog(apiClinkCallback.class);

    public String doCallBack(EliteBean eliteBean, HttpServletRequest request, HttpServletResponse response) {

        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json;charset=UTF-8");
        long startTime = System.currentTimeMillis();

        String getMethod = request.getMethod();
        log.debug("apiClinkCallback getMethod:" + getMethod);
        String outData = "FAIL";

        //获取所有参数名
        Enumeration ep = request.getParameterNames();
        while (ep.hasMoreElements()) {
            String parameterName = (String) ep.nextElement();
            log.debug(parameterName + ": " + request.getParameter(parameterName));
        }

        // 获取调用数据
        String callBody = Util.getRequestBody(request, "UTF-8");
        log.info("接收调用 POST 参数信息：" + callBody);

        // 获取数据源
        JdbcTemplate jt = new JdbcTemplate(eliteBean.getDataSource());

        // 解析调用推送方式
        JSONObject callJson = JSONObject.fromObject(callBody);

        // 话单推送呼入处理
        if (KHUtil.GetSFJV(callJson, "optype") == "话单推送_主话单推送" && KHUtil.GetSFJV(callJson, "callType") == "1") {
            toDoIBGet(jt, callJson);
        } else if (KHUtil.GetSFJV(callJson, "optype") == "话单推送_主话单推送" && KHUtil.GetSFJV(callJson, "callType") == "4") {
            toDoOBGet(jt, callJson);
        } else if (KHUtil.GetSFJV(callJson, "optype") == "来电推送_来电响铃" && KHUtil.GetSFJV(callJson, "callType") == "1") {
            toDoIBGet_Ring(jt, callJson);
        }

        return "SUCC";
    }

    // 1-呼入通话记录
    static toDoIBGet(JdbcTemplate jt, JSONObject dataObject) {

        //处理主数据
        log.debug("1-呼入通话记录 toDoGet Data callJson:" + dataObject);

        String mainUniqueId = KHUtil.GetSFJV(dataObject, "mainUniqueId");
        String uniqueId = KHUtil.GetSFJV(dataObject, "uniqueId");
        String hotline = KHUtil.GetSFJV(dataObject, "hotline");
        String customerNumber = KHUtil.GetSFJV(dataObject, "customerNumber");
        String customerProvince = KHUtil.GetSFJV(dataObject, "customerProvince");
        String customerCity = KHUtil.GetSFJV(dataObject, "customerCity");
        String qno = KHUtil.GetSFJV(dataObject, "qno");
        String callType = KHUtil.GetSFJV(dataObject, "callType");
        String startTime = KHUtil.GetSFJV(dataObject, "startTime");
        String endTime = KHUtil.GetSFJV(dataObject, "endTime");
        String bridgeTime = KHUtil.GetSFJV(dataObject, "bridgeTime");
        String recordFile = KHUtil.GetSFJV(dataObject, "recordFile");

        // 检查当前数据库是否存在 存在更新 不存在新建
        String sql = "Select Count(*) As Count From clink_list_cdr_ibs Where uniqueId = ?";
        List sqlResult = jt.queryForList(sql, uniqueId);
        int Countnum = sqlResult.get(0).get("Count");
        log.debug("1-呼入通话记录 检查当前数据库是否存在:" + Countnum);

        if (Countnum <= 0) {

            sql = '''Insert Into Clink_List_Cdr_Ibs
                          (Uniqueid,
                           Hotline,
                           Customernumber,
                           Customerprovince,
                           Customercity,
                           Qno,
                           Calltype,
                           Starttime,
                           Endtime,
                           Bridgetime,
                           Recordfile)
                        VALUES
                          (\'''' + uniqueId + '''\',
                           \'''' + hotline + '''\',
                           \'''' + customerNumber + '''\',
                           \'''' + customerProvince + '''\',
                           \'''' + customerCity + '''\',
                           \'''' + qno + '''\',
                           \'''' + callType + '''\',
                           \'''' + startTime + '''\',
                           \'''' + endTime + '''\',
                           \'''' + bridgeTime + '''\',
                           \'''' + recordFile + '''\')''';

            jt.update(sql);

        } else {
            sql = '''Update clink_list_cdr_ibs
                           SET hotline               = \'''' + hotline + '''\',
                               customerNumber        = \'''' + customerNumber + '''\',
                               customerProvince      = \'''' + customerProvince + '''\',
                               customerCity          = \'''' + customerCity + '''\',
                               qno                   = \'''' + qno + '''\',
                               callType              = \'''' + callType + '''\',
                               startTime             = \'''' + startTime + '''\',
                               endTime               = \'''' + endTime + '''\',
                               bridgeTime            = \'''' + bridgeTime + '''\',
                               recordFile            = \'''' + recordFile + '''\'
                         WHERE uniqueId = \'''' + uniqueId + '''\'''';

            jt.update(sql);
        }
    }

    // 3-呼出通话记录
    static toDoOBGet(JdbcTemplate jt, JSONObject dataObject) {

        //处理主数据
        log.debug("3-呼出通话记录 toDoGet Data dataObject:" + dataObject);

        String mainUniqueId = KHUtil.GetSFJV(dataObject, "mainUniqueId");
        String uniqueId = KHUtil.GetSFJV(dataObject, "uniqueId");
        String hotline = KHUtil.GetSFJV(dataObject, "hotline");
        String customerNumber = KHUtil.GetSFJV(dataObject, "customerNumber");
        String customerProvince = KHUtil.GetSFJV(dataObject, "customerProvince");
        String customerCity = KHUtil.GetSFJV(dataObject, "customerCity");
        String qno = KHUtil.GetSFJV(dataObject, "qno");
        String callType = KHUtil.GetSFJV(dataObject, "callType");
        String startTime = KHUtil.GetSFJV(dataObject, "startTime");
        String endTime = KHUtil.GetSFJV(dataObject, "endTime");
        String bridgeTime = KHUtil.GetSFJV(dataObject, "bridgeTime");
        String recordFile = KHUtil.GetSFJV(dataObject, "recordFile");

        // 检查当前数据库是否存在 存在更新 不存在新建
        String sql = "Select Count(*) As Count From clink_list_cdr_obs Where uniqueId = ?";
        List sqlResult = jt.queryForList(sql, uniqueId);
        int Countnum = sqlResult.get(0).get("Count");

        if (Countnum <= 0) {

            sql = '''Insert Into Clink_List_Cdr_Obs
                      (Uniqueid,
                       Hotline,
                       Customernumber,
                       Customerprovince,
                       Customercity,
                       Calltype,
                       Starttime,
                       Endtime,
                       Bridgetime,
                       Recordfile,
                       Qno)
                    Values
                      (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)''';

            jt.update(sql, uniqueId, hotline, customerNumber, customerProvince, customerCity, callType, startTime, endTime, bridgeTime, recordFile, qno);

        } else {
            sql = '''Update Clink_List_Cdr_Obs
                               Set Hotline               = ?,
                                   Customernumber        = ?,
                                   Customerprovince      = ?,
                                   Customercity          = ?,
                                   Calltype              = ?,
                                   Starttime             = ?,
                                   Endtime               = ?,
                                   Bridgetime            = ?,
                                   Recordfile            = ?,
                                   Qno                   = ?
                             Where Uniqueid = ?''';

            jt.update(sql, hotline, customerNumber, customerProvince, customerCity, callType, startTime, endTime, bridgeTime, recordFile, qno, uniqueId);
        }

    }

    // 3-来电推送_来电响铃
    static toDoIBGet_Ring(JdbcTemplate jt, JSONObject dataObject) {

        //处理主数据
        log.debug("3-来电推送_来电响铃 toDoGet Data callJson:" + dataObject);

        String ringTime = KHUtil.GetSFJV(dataObject, "ringTime");
        String consulterCno = KHUtil.GetSFJV(dataObject, "consulterCno");
        String cno = KHUtil.GetSFJV(dataObject, "cno");
        String hotline = KHUtil.GetSFJV(dataObject, "hotline");
        String customerNumber = KHUtil.GetSFJV(dataObject, "customerNumber");
        String callType = KHUtil.GetSFJV(dataObject, "callType");
        String optype = KHUtil.GetSFJV(dataObject, "optype");
        String bindTel = KHUtil.GetSFJV(dataObject, "bindTel");
        String customerNumberType = KHUtil.GetSFJV(dataObject, "customerNumberType");
        String qno = KHUtil.GetSFJV(dataObject, "qno");
        String numberTrunk = KHUtil.GetSFJV(dataObject, "numberTrunk");
        String customerNumberAreaCode = KHUtil.GetSFJV(dataObject, "customerNumberAreaCode");
        String mainUniqueId = KHUtil.GetSFJV(dataObject, "mainUniqueId");
        String transferCno = KHUtil.GetSFJV(dataObject, "transferCno");
        String enterpriseId = KHUtil.GetSFJV(dataObject, "enterpriseId");

        // 检查当前数据库是否存在 存在更新 不存在新建
        String sql = '''Replace Into Clink_List_Cdr_Ring
                          (Rec_Guid,
                           Enterpriseid,
                           Mainuniqueid,
                           Hotline,
                           Cno,
                           Bindtel,
                           Customernumber,
                           Customernumbertype,
                           Customernumberareacode,
                           Calltype,
                           Numbertrunk,
                           Ringtime,
                           Qno,
                           Consultercno,
                           Transfercno,
                           Update_Time)
                        Values
                          (Uuid(), ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, Now())''';
        int Countnum = jt.update(sql, enterpriseId, mainUniqueId, hotline, cno, bindTel, customerNumber, customerNumberType, customerNumberAreaCode, callType, numberTrunk, ringTime, qno, consulterCno, transferCno);
        log.debug("3-来电推送_来电响铃 数据入库结果:" + Countnum);

    }

}